﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CrossEntropyProject
{
    internal static class PamMatrixInput
    {

        #region Class variables
        internal static float[][] CDFarrays ;
        internal static float[,] distanceMatrix;
        #endregion


        #region CalcC_D_F()

        internal static void CalcC_D_F()
        { 
            int[] hasharr;
            Int32 numberOfCustomers = DataSet.numOfCustomers;
            CDFarrays = new float[Constants.numOfhoursInDay][];


            for (int col = 1; col < Constants.numOfhoursInDay; col++)
            {
                float[] camulArr = new float[DataSet.arrCallsMax[col]+1];
                int[] countingArr = new int[DataSet.arrCallsMax[col]+1];

                foreach (DictionaryEntry entry in DataSet.customersHashTable)
                {
                    hasharr = ((int[])entry.Value);
                    countingArr[hasharr[col]]++;
                }

                for (int k = 1; k < countingArr.Length; k++)
                {
                    countingArr[k] = countingArr[k - 1] + countingArr[k];
                }

                for (int k = 0; k < camulArr.Length; k++)
                {
                    camulArr[k] = (float)((float)countingArr[k] / (float)numberOfCustomers);
                }

                CDFarrays[col] = camulArr; 
            }//for col

          }//CDFcalc
    
        #endregion


        #region InitPAMmatrix()
        internal static void InitPAMmatrix()
        {

            distanceMatrix = new float[Constants.numOfhoursInDay, Constants.numOfhoursInDay];
            distanceMatrix[0,0] = -1;

            for (int i = 1; i < Constants.numOfhoursInDay; i++)
            {
                for (int j = 1; j < Constants.numOfhoursInDay; j++)
                {
                    distanceMatrix[i,j]=FindDistance(CDFarrays[i], CDFarrays[j]);
                }
            }

        }
        #endregion


        #region FindDistance(float[] hourI, float[] hourJ)
        internal static float FindDistance(float[] hourI, float[] hourJ)
        {
            float maxdistance = 0 ;
            float temp;
            int length = 0;

            if (hourI.Length > hourJ.Length)
              length = hourJ.Length;
            else
              length = hourI.Length;


            for (int i = 0; i < length; i++)
            {
                temp = (float)(Math.Abs(hourI[i] - hourJ[i]));
                if (temp > maxdistance)
                {
                   maxdistance = temp;
                }
             }
            return maxdistance;
        }
        #endregion


        #region Clear()
        internal static void Clear()
        {
            if (CDFarrays != null)
            {
                Array.Clear(CDFarrays, 0, CDFarrays.Length);  
            }
            if (distanceMatrix != null)
            {
                Array.Clear(distanceMatrix, 0, distanceMatrix.Length);
            }

            
        }
        #endregion

  
    }//class
}// name space

